package com.nervenets.web.hibernate.domain;

import com.nervenets.Global;
import com.nervenets.utils.JodaUtils;
import com.nervenets.web.enumeration.Bank;
import com.nervenets.web.enumeration.OrderStatus;
import com.nervenets.web.enumeration.PayType;
import com.nervenets.web.hibernate.DomainObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Type;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单表
 */
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "orders")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order extends DomainObject {
    private long userId;//订单用户ID
    private long promotionId;//活动ID
    private long managerId;//活动方ID
    private OrderStatus status = OrderStatus.init;//订单状态
    private PayType payType;//支付类型
    private long payMoney;//订单金额
    private long giveMoney;//返现金额
    private int payTime;//支付时间
    private Bank payBank;
    private String validationMobile;
    private String mobile;
    private String bankNumber;
    private String idNumber;
    @Type(type = "com.nervenets.web.adapter.jsonType.OrderFlowJson")
    @Column(columnDefinition = "text")
    private List<OrderFlow> flows = new ArrayList<>();

    public void addFlow(OrderStatus beforeStatus, OrderStatus afterStatus, String detail, boolean hidden) {
        if (null == this.flows) this.flows = new ArrayList<>();
        this.flows.add(new OrderFlow(JodaUtils.getTimestamp(), beforeStatus, afterStatus, detail, hidden));
    }

    public String orderNumber() {
        return Global.OrderNumber.getNormalService(getId(), getCreateTime());
    }
}
